﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using BusinessObjects;

public partial class controls_admin_ViewCategory : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (divMessage.InnerHtml != "")
            divMessage.InnerHtml = "";
        if (!IsPostBack)
            getCategories();
    }
    protected void lbCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            base.OpenConnection();
            if (!lbCategory.SelectedValue.Equals("0") && lbCategory.SelectedItem != null)
            {
                int categoryID = Convert.ToInt32(lbCategory.SelectedValue);
                DataTable objDT = BO_HelpDetail.SearchByCategory(categoryID);
                if (objDT != null && objDT.Rows.Count > 0)
                    fkcContent.Value = objDT.Rows[0]["HELP_DETAILS"].ToString();
                else
                    BO_HelpDetail.Insert(categoryID, "", DateTime.Now, "", DateTime.Now, "", true);
            }
        }
        catch (Exception ex) { throw ex; }
        finally { base.CloseConnection(); }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            base.OpenConnection();
            if (lbCategory.SelectedItem == null && lbCategory.SelectedValue.Equals("0"))
            {
                divMessage.InnerHtml = O2S_Message.Warning("Cảnh Báo", "Bạn phải chọn danh mục trợ giúp");
                return;
            }
            string details = fkcContent.Value;
            DataTable objDT = BO_HelpDetail.SearchByCategory(Convert.ToInt32(lbCategory.SelectedValue));
            BO_HelpDetail.Update(
                Convert.ToInt32(objDT.Rows[0]["HELP_ID"]), Convert.ToInt32(lbCategory.SelectedValue), details,
                Convert.ToDateTime(objDT.Rows[0]["CREATED_DATE"]), objDT.Rows[0]["CREATED_BY"].ToString(),
                DateTime.Now, "", true);
            divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thành công !");
            lbCategory.SelectedIndex = 0;
            getCategories();
        }
        catch { }
        finally { base.CloseConnection(); }
    }

    //protected void btnDel_Click(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        if (!lbCategory.SelectedValue.Equals("0") && lbCategory.SelectedValue != null)
    //        {
    //            int categoryID = Convert.ToInt32(lbCategory.SelectedValue);
    //            if (BO_HelpCategories.Delete(categoryID))
    //                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Xóa thành công !");
    //            else
    //                divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Chưa thực hiện được !");
    //        }
    //        else
    //            divMessage.InnerHtml = O2S_Message.Error("Yêu Cầu", "Chọn danh mục muốn xóa !");
    //        lbCategory.SelectedIndex = 0;
    //        getCategories();
    //    }
    //    catch { }
    //}
    protected void getCategories()
    {
        try
        {
            base.OpenConnection();
            lbCategory.Items.Clear();
            lbCategory.Items.Add(new ListItem("Danh mục cao nhất", "0"));
            DataTable objDT = BO_HelpCategories.SearchByParent(0);
            if (objDT.Rows.Count > 0)
            {
                for (int i = 0; i < objDT.Rows.Count; i++)
                {
                    string ident = "---";
                    lbCategory.Items.Add(new ListItem(ident + objDT.Rows[i]["CATEGORY_NAME"].ToString(), objDT.Rows[i]["CATEGORY_ID"].ToString()));
                    getChild_Categories(Convert.ToInt32(objDT.Rows[i]["CATEGORY_ID"].ToString()), ident);
                }
                lbCategory.SelectedIndex = 0;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    protected void getChild_Categories(int _parent_id, string ident)
    {
        try
        {
            DataTable objDT = BO_HelpCategories.SearchByParent(_parent_id);
            if (objDT.Rows.Count > 0)
            {
                ident += "--- ";
                for (int i = 0; i < objDT.Rows.Count; i++)
                {
                    lbCategory.Items.Add(new ListItem(ident + objDT.Rows[i]["CATEGORY_NAME"].ToString(), objDT.Rows[i]["CATEGORY_ID"].ToString()));
                    getChild_Categories(Convert.ToInt32(objDT.Rows[i]["CATEGORY_ID"].ToString()), ident);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
